package com.struts.utils;

import java.sql.DriverManager;

import com.mysql.jdbc.Connection;

public class DBUtils {
	
	private static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
	private static final String DBURL = "jdbc:mysql://localhost:3306/db_struts?useUnicode=true&characterEncoding=utf8" ;
	private static final String DBUSER = "root" ;
	private static final String DBPASSWORD = "P@ssw0rd" ;
	private static Connection conn = null ;
	
	static {
		try {
			Class.forName(DBDRIVER) ;
			conn = (Connection) DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	//获取数据库连接
	public static Connection getConnection() {
		return conn ;
	}
	
	//关闭数据库连接
	public void close() {
		try {
			this.conn.close() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

}
